package com.spring.sunflower.dialog

import android.content.Context
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.google.android.flexbox.FlexboxLayout
import com.lxj.xpopup.core.CenterPopupView
import com.makeramen.roundedimageview.RoundedImageView
import com.spring.sunflower.R
import com.spring.sunflower.bean.HelloBean
import com.spring.sunflower.util.DisplayUtils
import com.spring.sunflower.util.DoubleClickUtils
import kotlinx.android.synthetic.main.layout_item_hello.view.*
import java.util.*

/**
 * Created by Kevin on 2021/8/6<br/>
 *
 * Describe:<br/>
 */
class HelloDialog(
    context: Context,
    var layoutID: Int,
) : CenterPopupView(context) {
    private var tvDone: TextView? = null
    private var fbl: FlexboxLayout? = null
    private var hasSigned = false
    private var mTimer: Timer? = null
    private var mHandler: Handler? = null
    private var mRunnable: Runnable? = null
    override fun getImplLayoutId(): Int {
        return layoutID
    }

    override fun onCreate() {
        super.onCreate()
        fbl = findViewById(R.id.fbl)
        tvDone = findViewById(R.id.tvDone)
        val ivClose = findViewById<ImageView>(R.id.ivClose)
        ivClose.setOnClickListener { dismiss() }
        listener?.onCreateView()
        mTimer = Timer()
        mRunnable = Runnable { dismiss() }
        mHandler = Handler(Looper.getMainLooper())
        mHandler?.postDelayed(mRunnable!!, 15000)
        tvDone?.setOnClickListener {
            if (DoubleClickUtils.isFastLongClick()) return@setOnClickListener
            mRunnable?.let {
                mHandler?.removeCallbacks(it)
            }
            listener?.onSayHello()
        }
    }

    private var listener: OnSayHelloListener? = null
    fun setOnSayHelloListener(l: OnSayHelloListener) {
        listener = l
    }

    interface OnSayHelloListener {
        fun onSayHello()//
        fun onCreateView()
    }

    fun addHelloObject(data: HelloBean.DatasBean) {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_item_hello, null)
        val rivAvatar = view.findViewById<RoundedImageView>(R.id.rivAvatar)
        val tvNickName: TextView = view.findViewById(R.id.tvNickname)
        val tvAge: TextView = view.findViewById(R.id.tvAge)
        val llContainer: LinearLayout = view.findViewById(R.id.llContainer)
        val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, -2)
        layoutParams.width = (DisplayUtils.getScreenWidth(context) * 0.40).toInt()
        view.layoutParams = layoutParams
        Glide.with(context)
            .load(data.photo)
            .placeholder(R.drawable.ic_placeholder)
            .error(R.drawable.ic_placeholder)
            .into(rivAvatar)
        tvNickName.text = data.nickname
        tvAge.text = "${data.age}岁"
        fbl!!.addView(view)
    }

    override fun getMaxWidth(): Int {
        return (DisplayUtils.getScreenWidth(context) * 0.95).toInt()
    }

}